<?php

namespace app\model;

use app\stateData;
use think\Model;

class SystemBusiness extends Model
{

    /**
     * 业务表
     */
    protected $createTime='create_date';

    public function getBusinessTypeAttr($value): string
    {
        $status = stateData::$_business_type;
        return $status[$value];
    }

    /**
     * 查询业务列表
     * @param int $page
     * @return SystemBusiness[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBusinessListModel($business_name,$pageNum,int $page=1):array
    {
        return $this->whereLike ('business_name','%'.$business_name.'%')->page($page,$pageNum)->order ('create_date DESC')->select()->toArray ();
    }
    public function getAllBusinessListModel():array
    {
        return $this->where(['is_k'=>0])->order ('create_date ASC')->select()->toArray ();
    }

    /**
     * 获取当前模型总页数
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getPageTotal($business_name): int
    {
        return $this->whereLike ('business_name','%'.$business_name.'%')->count();
    }

    /**
     * 添加证书
     * @param $data
     * @return SystemBusiness|Model
     */
    public function addBusinessModel($data)
    {
        return SystemBusiness::create($data);
    }

    /**
     * 修改证书
     * @param $data
     * @return SystemBusiness
     */
    public function editBusinessModel($data)
    {
        return SystemBusiness::update($data);
    }


    /**
     * 删除证书
     * @param $data
     * @return bool
     */
    public function delBusinessModel($data): bool
    {
        return self::destroy($data);
    }

    /**
     * 按业务名称模糊查询
     */

    public function getBusNameSearch($where)
    {
        return $this->whereLike('business_name','%'.$where.'%')->select();
    }

    /**
     * 查询where business_category
     * @param $business_category
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWhereBusinessNameModel($business_category): array
    {
        return $this->where(['business_category'=>$business_category])->field('business_name,id,head_id')->select()->toArray ();
    }

    public function getWhereBusinessCategoryModel($business_category): array
    {
        return $this->where(['business_category'=>$business_category])->field('business_category as business_name,id,head_id')->select()->toArray ();
    }

    public function getWhereBusinessNameAsTitleModel($business_category): array
    {
        return $this->where(['business_category'=>$business_category])->field('business_name as title,id')->select()->toArray ();
    }

    /**
     * 获取业务负责人
     * @param $id
     * @return \app\model\SystemBusiness|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWhereIdDataModel($id)
    {
        return $this->where (['id'=>$id])->value('head_id');
    }

    /**
     * 获取正常开启的业务
     * @param $type
     * @return \app\model\SystemBusiness[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSubsidiaryBusinessListModel($type=2)
    {
        return $this->where (['business_type'=>$type,'is_k'=>0])->select();
    }

    /**
     * 根据业务id查询业务名称
     * @param $id
     * @return mixed
     */
    public function getBusinessIdTitle($id)
    {
        return $this->where (['id'=>$id])->value ('business_name');
    }

    /**
     * 通过证书名称查询ID
     * @param $name
     * @return mixed
     */
    public function getBusinessIdModel($name)
    {
        return $this->where (['business_name'=>$name])->value ('id');
    }
    /**
     * 获取type 所有业务名称
     * @param $type
     * @return array
     */
    public function getSubTypeArrDataModel(): array
    {
        return $this->where('is_k','0')->column ('business_name,id,business_category');
    }

    public function getBusinessCategoryModel($id)
    {
        return $this->where (['id'=>$id])->value ('business_category');
    }

    public function getBNameCategoryModel($name)
    {
        return $this->where(['business_name'=>$name])->whereRaw ("business_category != ''")->field('id,business_category as title')->select()->toArray ();
    }



}